package com.fuhao.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.fuhao.base.BaseWebController;
import com.fuhao.constants.WebConstants;
import com.fuhao.core.Constants.Constants;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.dto.input.MemberLoginInpDTO;
import com.fuhao.feign.MemberLoginServiceFeign;
import com.fuhao.utils.CookieUtils;
import com.fuhao.utils.RandomValidateCodeUtil;
import com.fuhao.utils.VoBeanUtils;
import com.fuhao.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author zhang
 * @title: 登录
 */
@Controller
public class LoginContorller extends BaseWebController {

    /**
     * 跳转到登陆页面页面
     */
    private static final String MB_LOGIN_FTL = "member/login";
    private static final String MB_QQ_QQLOGIN = "member/qqlogin";
    /**
     * 重定向到首页
     */
    private static final String REDIRECT_INDEX = "redirect:/";

    @Autowired
    private MemberLoginServiceFeign memberLoginServiceFeign;

    /**
     * 跳转到登录页面
     * @return
     */
    @GetMapping("/login")
    public String getLogin(){
        return MB_LOGIN_FTL;
    }

    @PostMapping("/login")
    public String postLogin(@ModelAttribute("loginVo")LoginVo loginVo, Model model, HttpServletRequest request,
                            HttpServletResponse response, HttpSession httpSession){
        // 1.图形验证码判断
        String graphicCode = loginVo.getGraphicCode();
        if (!RandomValidateCodeUtil.checkVerify(graphicCode, httpSession)) {
            setErrorMsg(model, "图形验证码不正确!");
            return MB_LOGIN_FTL;
        }
        //2.登录
        //2.1 vo转dto
        MemberLoginInpDTO memberLoginInpDTO = VoBeanUtils.voToDto(loginVo, MemberLoginInpDTO.class);
        // 1.判断登录方式
        String qqOpenId = (String) httpSession.getAttribute(WebConstants.LOGIN_QQ_OPENID);
        memberLoginInpDTO.setQqOpenId(qqOpenId);
        memberLoginInpDTO.setLoginType(Constants.MEMBER_LOGIN_TYPE_PC);
        memberLoginInpDTO.setDeviceInfor(webBrowserInfo(request));
        BaseResponse<JSONObject> loginInfo = memberLoginServiceFeign.login(memberLoginInpDTO);
        if(!isSuccess(loginInfo)){
            setErrorMsg(model,loginInfo.getMsg());
            return MB_LOGIN_FTL;
        }
        //3.将token存到cookie中
        JSONObject data = loginInfo.getData();
        String token = data.getString("token");
        CookieUtils.setCookie(request, response, WebConstants.LOGIN_TOKEN_COOKIENAME, token);
        // 登陆成功后，跳转到首页
        return REDIRECT_INDEX;

    }

//    /**
//     * qq登录
//     * @param loginVo
//     * @param model
//     * @param request
//     * @param response
//     * @param httpSession
//     * @return
//     */
//    @RequestMapping("/qqJointLogin")
//    public String qqJointLogin(@ModelAttribute("loginVo") LoginVo loginVo, Model model, HttpServletRequest request,
//                               HttpServletResponse response, HttpSession httpSession) {
//
//        // 1.获取用户openid
//        String qqOpenId = (String) httpSession.getAttribute(WebConstants.LOGIN_QQ_OPENID);
//        if (StringUtils.isEmpty(qqOpenId)) {
//            return ERROR_500_FTL;
//        }
//
//        // 2.将vo转换dto调用会员登陆接口
//        MemberLoginInpDTO userLoginInpDTO = VoBeanUtils.voToDto(loginVo, MemberLoginInpDTO.class);
//        userLoginInpDTO.setQqOpenId(qqOpenId);
//        userLoginInpDTO.setLoginType(Constants.MEMBER_LOGIN_TYPE_PC);
//        String info = webBrowserInfo(request);
//        userLoginInpDTO.setDeviceInfor(info);
//        BaseResponse<JSONObject> login = memberLoginServiceFeign.login(userLoginInpDTO);
//        if (!isSuccess(login)) {
//            setErrorMsg(model, login.getMsg());
//            return MB_QQ_QQLOGIN;
//        }
//        // 3.登陆成功之后如何处理 保持会话信息 将token存入到cookie 里面 首页读取cookietoken 查询用户信息返回到页面展示
//        JSONObject data = login.getData();
//        String token = data.getString("token");
//        CookieUtils.setCookie(request, response, WebConstants.LOGIN_TOKEN_COOKIENAME, token);
//        return REDIRECT_INDEX;
//    }


}
